<!-- +----------------------------------------------------------------------
| 麦沃德科技赋能开发者，助力中小企业发展 
+----------------------------------------------------------------------
| Copyright (c) 2017～2024  www.wdadmin.cn    All rights reserved.
+----------------------------------------------------------------------
| 沃德代驾系统并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
+----------------------------------------------------------------------
| Author: MY WORLD Team <bd@maiwd.cn>   www.wdadmin.cn
+----------------------------------------------------------------------
| 开票历史 开发者: 麦沃德科技-半夏
+---------------------------------------------------------------------- -->

<template>
	<view class="container" :style="{'--theme-color': themeColor}" v-if="loadEnd">
		<view class="container-list" v-if="historyList.length">
			<view class="list-item" v-for="item in historyList" :key="item.id">
				<view class="item-group">
					<view class="group-title">开票方</view>
					<view class="group-value">{{invoiceName}}</view>
				</view>
				<view class="item-group">
					<view class="group-title">发票总额</view>
					<view class="group-value"><text class="text">{{item.fees}}</text>元</view>
				</view>
				<view class="item-group">
					<view class="group-title">开票类型</view>
					<view class="group-value">
						<text class="text" v-if="item.invoice_type == 1">普通发票{{item.title_type == 1 ? '-个人' : item.title_type == 2 ? '-单位' : ''}}</text>
						<text class="text" v-else-if="item.invoice_type == 2">专用发票</text>
					</view>
				</view>
				<view class="item-group flex justify-content-end" v-if="item.file">
					<view class="group-btn" @click="handleDownload(item.file)">下载发票</view>
				</view>
				<view class="item-mask" v-if="loading">
					<view class="mask-box">
						<view class="box-left"></view>
						<view class="box-right"></view>
					</view>
					<view class="mask-box">
						<view class="box-left"></view>
						<view class="box-right"></view>
					</view>
					<view class="mask-box">
						<view class="box-left"></view>
						<view class="box-right"></view>
					</view>
					<view class="mask-bottom" v-if="item.file">
						<view class="bottom-right"></view>
					</view>
				</view>
			</view>
		</view>
		<empty top="30%" title="暂无相关内容~" v-else></empty>
	</view>
</template>

<script>
	import { mapState } from "vuex"
	export default {
		data() {
			return {
				// 加载完成
				loadEnd: false,
				// 开票方
				invoiceName: "",
				// 分类查询参数
				page: 1,
				limit: 20,
				hasMore: false,
				// 历史列表
				historyList: [],
				// 列表是否加载
				loading: false,
			}
		},
		computed: {
			...mapState({
				themeColor: state => state.app.themeColor,
				shareTitle: state => state.shareTitle,
				shareImage: state => state.shareImage,
			})
		},
		onLoad() {
			uni.showLoading({
				title: "加载中"
			})
			this.getHistoryList(() => {
				uni.hideLoading()
				this.loadEnd = true
			})
		},
		onPullDownRefresh() {
			this.page = 1
			this.loading = true
			this.getHistoryList(() => {
				this.loading = false
				uni.stopPullDownRefresh()
			})
		},
		onReachBottom() {
			if (this.hasMore) {
				this.page++
				this.getHistoryList()
			}
		},
		methods: {
			// 获取发票列表
			getHistoryList(fn) {
				this.$util.request("passenger.invoice.list", {
					page: this.page,
					limit: this.limit,
				}).then(res => {
					if (fn) fn()
					if (res.code == 1) {
						this.invoiceName = res.data.invoicing_party
						let list = res.data.data
						this.hasMore = this.page < res.data.count / this.limit ? true : false
						this.historyList = this.page == 1 ? list : [...this.historyList, ...list];
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(error => {
					if (fn) fn()
					console.error('获取发票列表 ', error)
				})
			},
			// 下载发票
			handleDownload(link) {
				if (link) {
					uni.showLoading({
						mask: true,
						title: '加载中',
					})
					uni.getImageInfo({
						src: link,
						success: (res) => {
							if (res.type == "unknown" || res.width == -1 || res.height == -1) {
								this.openDocument(link)
							} else {
								uni.previewImage({
									urls: [link],
									success: () => {
										uni.hideLoading()
									},
								})
							}
						},
						fail: () => {
							this.openDocument(link)
						},
					})
				} else {
					uni.showToast({
						icon: 'none',
						title: '暂无可查看发票',
					})
				}
			},
			// 打开文件
			openDocument(link) {
				this.$util.openDocument(link).then(() => {
					uni.hideLoading()
				}).catch(() => {
					uni.hideLoading()
					uni.showToast({
						icon: 'none',
						title: '发票打开失败',
					})
				})
			},
		}
	}
</script>

<style lang="scss">
	.container {
		padding: 32rpx;

		.container-list {
			.list-item {
				position: relative;
				margin-top: 32rpx;
				border-radius: 32rpx;
				overflow: hidden;
				background: #FFF;
				padding: 32rpx;

				&:first-child {
					margin-top: 0;
				}

				.item-group {
					display: flex;
					align-items: center;
					margin-top: 32rpx;

					&:first-child {
						margin-top: 0;
					}

					.group-title {
						color: #242629;
						font-size: 28rpx;
						font-weight: 600;
						line-height: 40rpx;
					}

					.group-value {
						margin-left: 32rpx;
						color: #242629;
						font-size: 28rpx;
						font-weight: 600;
						line-height: 40rpx;
						text-align: right;
						flex: 1;

						.text {
							color: var(--theme-color);
						}
					}

					.group-btn {
						color: #FFF;
						font-size: 24rpx;
						font-weight: 600;
						line-height: 44rpx;
						padding: 12rpx 40rpx;
						border-radius: 40rpx;
						background: var(--theme-color);
						min-width: 128rpx;
					}
				}

				.item-mask {
					position: absolute;
					top: 0;
					left: 0;
					right: 0;
					bottom: 0;
					z-index: 9;
					background: #FFF;
					display: flex;
					flex-direction: column;
					justify-content: center;
					padding: 32rpx;

					.mask-box {
						display: flex;
						margin-top: 32rpx;

						&:first-child {
							margin-top: 0;
						}

						.box-left {
							width: 25%;
							height: 40rpx;
							background: #eee;
						}

						.box-right {
							margin-left: 32rpx;
							flex: 1;
							height: 40rpx;
							background: #eee;
						}
					}

					.mask-bottom {
						display: flex;
						justify-content: flex-end;
						margin-top: 32rpx;

						.bottom-right {
							width: 100%;
							height: 68rpx;
							background: #eee;
						}
					}
				}
			}
		}
	}
</style>